<?php
namespace app\wap\controller;
use \think\Controller;

class Weixin extends Controller{
	public $weixin;
	public $config;

	public function index(){
		$config = model('Config')->_get('', 'weixin');
		$token = $config['token'];
		if(!$token){
			exit('token no setting!');
		}
		$this->config = $config;

		import('ThinkWechat', EXTEND_PATH, '.class.php');
		$this->weixin = new \ThinkWechat($token);
		$this->weixin->appid  = $config['appid'];
		$this->weixin->secret = $config['appsecret'];

		/* 获取请求信息 */
		$data = $this->weixin->request();

		/* 获取回复信息 */
		list($content, $type) = $this->reply($data);

		/* 响应当前请求 */
		$this->weixin->response($content, $type);
	}

	private function reply($data){
		if($msg_type = $data['MsgType']){
			switch ($msg_type) {
				case 'text':
					$reply = $this->handleText($data);
					break;
				
				case 'event':
					$reply = $this->handleEvent($data);
					break;
				
				case 'voice':
					$reply = $this->handleVoice($data);
					break;
				
				default:
					# code...
					break;
			}
		}

		if(!$reply){
			exit;
		}
		return $reply;
	}

	//消息文本回复
	private function handleText($data){
		$content = trim($data['Content']);
		$openid  = $data['FromUserName'];
		$model   = model('WeixinInput');
		$model->weixin_openid = $openid;//设置微信用户账号
		$model->weixin_input  = $content;//设置微信输入内容
		
		return $model->index();
	}
	//处理事件回复
	private function handleEvent($data){
		$weixin_model = model('Weixin');
		$openid = $data['FromUserName'];
		//菜单栏目点击事件
		if($data['Event'] == 'CLICK'){
			$eventkey = $data['EventKey'];

			$keys = explode('_', $eventkey);
			
			$mod     = $keys[0];
			$act     = $keys[1];
			$content = str_replace($mod . '_' . $act, '', $eventkey);
			$content = substr($content, 1);

			$model = model($mod);
			$model->weixin_openid = $openid;//设置微信用户账号
			$model->weixin_input  = $content;//设置微信输入内容

			$reply = $model->$act();
			
		}
		// 扫描事件
		if($data['Event'] == 'SCAN'){
			$scene_id = $data['EventKey'];
			$content  = $weixin_model->get_scan_info($scene_id, $openid);
			$reply    = array($content, 'text');
			// 保存当前用户扫描ID
			cache('scene_id_openid_' . $openid, $scene_id);
		}
		//关注账号推送消息
		if($data['Event'] == 'subscribe'){
			$content = $this->config['welcome'];
			$reply = array($content, 'text');
			//获取用户信息
			$user_info = $this->weixin->get_openid_userinfo($openid);
			$unionid   = $user_info['unionid'];
			$weixin_model->update_user(
				$openid,
				$user_info['unionid'],
				$user_info['nickname'],
				$user_info['sex'],
				$user_info['province'],
				$user_info['city'],
				$user_info['country'],
				$user_info['headimgurl']
			);
			$event_key = explode('_', $data['EventKey']);
			// 扫描场景处理
			if($event_key[0] == 'qrscene'){
				$scene_id = $event_key[1];
				$content  = $weixin_model->get_scan_info($scene_id, $openid, $unionid);
				$reply    = array($content, 'text');
				// 保存当前用户扫描ID
				cache('scene_id_openid_' . $openid, $scene_id);
			}
		}
		//取消关注推送消息
		if($data['Event'] == 'unsubscribe'){
			$weixin_model->unsubscribe($openid);
		}
		
		return $reply;
	}
	//语音消息处理
	private function handleVoice($data){
		
	}
}
